#include <cstdlib>
#include <iostream>
#include <hdfs.h>

using namespace std;

int main(int argc, char **argv) {


    /**
        If the string given is 'default', the default NameNode
        configuration will be used (from the XML configuration files)
        If NULL is given, a LocalFileSystem will be created.
        If the string starts with a protocol type such as file:// or
        hdfs://, this protocol type will be used.  If not, the
        hdfs:// protocol type will be used.
        You may specify a NameNode port in the usual way by
        passing a string of the format hdfs://<hostname>:<port>.
        Alternately, you may set the port with
        hdfsBuilderSetNameNodePort.  However, you must not pass the
        port in two different ways.
        See hdfsBuilderSetNameNode for details.
        如果在classpath中没有包含hdfs-site.xml文件所在的路径，那么即使配置了default只会读取本地文件系统
     */
    hdfsFS fs = hdfsConnect("default",0);
    if (!fs){
        cout <<"cannot connect to HDFS！"<<endl;
        exit(1);
    }
    cout <<"connect success!"<<endl;

    const char* file_path = "/tmp/version.txt";

    hdfsFile readFile = hdfsOpenFile(fs, file_path, O_RDONLY, 0, 0, 0);
    if (!readFile){
        cout <<"Failed to open file！"<<endl;
        exit(1);
    }
    char* buffer = new char[100];
    int bufferSize = 100;

    tSize size = hdfsRead(fs, readFile, (void*)buffer, bufferSize);
    if (size < 0){
        cout << "Failed to read file!" << endl;
    }

    cout << "len:" << size << endl;
    string str(buffer, size);
    cout << str << endl;

    delete[] buffer;
    hdfsCloseFile(fs,readFile);
    return 0;

}